package com.controller.sys.user;

import com.common.ErrorCodeEnum;
import com.common.utils.StringUtils;
import com.core.email.EmailService;
import com.model.base.Result;
import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.exception.BusinessException;
import com.common.utils.Md5Utils;
import com.core.sms.service.SmsServer;
import com.model.dto.sys.userController.RegisterUserDTO;
import com.model.pojo.sys.SysUser;
import com.service.sys.SysUserRoleService;
import com.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Objects;

/**
 * @Author: zql
 * @Description: 注册用户管理
 * @Data: 15:02 2021/7/21
 */
@Api(tags = {"个人中心"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class RegisterController {

    @Resource
    SysUserService sysUserService;

    @Resource
    SmsServer smsServer;

    @Resource
    SysUserRoleService sysUserRoleService;

    @Resource
    EmailService emailService;

    /**
     * @Author: zql
     * @Description: 注册用户管理，目前没有操作日志记录
     * @param registerUserDTO 注册信息
     * @Data: 16:20 2021/7/21
     */
    @ApiOperation(value = "注册用户")
    @Log(title = "注册用户", businessType = BusinessType.INSERT)
    @PostMapping("/registerUser")
    public Result<Objects> registerUser(@RequestBody @Valid RegisterUserDTO registerUserDTO) {
        if(StringUtils.isNotEmpty(registerUserDTO.getEmail())) {
            if (!emailService.check(registerUserDTO.getEmail(), registerUserDTO.getEmailCode())) {
                throw new BusinessException(ErrorCodeEnum.EMAIL_CODE_VERIFY_FAILED);
            }
            if (sysUserService.hasEmail(registerUserDTO.getEmail())) {
                log.error("邮箱重复:{}", registerUserDTO.getEmail());
                throw new BusinessException(ErrorCodeEnum.USER_EMAIL_EXISTS);
            }
        }
        if(StringUtils.isNotEmpty(registerUserDTO.getMobile())) {
            if (!smsServer.check(registerUserDTO.getMobile(), registerUserDTO.getMobileCode())) {
                throw new BusinessException(ErrorCodeEnum.MOBILE_CODE_VERIFY_FAILED);
            }
            if (sysUserService.hasMobile(registerUserDTO.getMobile())) {
                log.error("手机号重复:{}", registerUserDTO.getMobile());
                throw new BusinessException(ErrorCodeEnum.USER_MOBILE_EXISTS);
            }
        }
        if (sysUserService.hasName(registerUserDTO.getName())) {
            log.error("登录用户名重复:{}", registerUserDTO.getName());
            throw new BusinessException(ErrorCodeEnum.USER_NAME_EXISTS);
        }
        SysUser sysUser = new SysUser();
        sysUser.setSysDeptId(1L);
        BeanUtils.copyProperties(registerUserDTO, sysUser);
        sysUser.setPassword(sysUserService.encode(Md5Utils.toUpperCase32(sysUser.getPassword())));
        sysUserService.register(sysUser);
        //默认分配1号角色
        sysUserRoleService.bindingRole(1L, Collections.singleton(sysUser.getSysUserId()));
        return Result.suc();
    }
}
